<template>
  <div class="app-container">
    <div class="query">
      <div>
        <el-button size="mini" type="primary" icon="el-icon-plus" circle @click=" form = {};dialogVisible = true;editPassword = true" />
      </div>
      <div>
        <el-button size="mini" icon="el-icon-refresh" circle @click="initList" />
      </div>
    </div>
    <el-table :data="list" border fit highlight-current-row row-key="id" :tree-props="{ children: 'child', hasChildren: 'hasChildren' }">
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">{{ scope.$index + 1 }}</template>
      </el-table-column>
      <!-- <el-table-column align="center" label="所属分类id" width="120">
        <template slot-scope="scope">{{ scope.row.cid }}</template>
      </el-table-column> -->
      <el-table-column align="center" label="分类名称" width="200">
        <template slot-scope="scope">{{ scope.row.typename }}</template>
      </el-table-column>
      <el-table-column align="center" label="分类介绍" width="300">
        <template slot-scope="scope">{{ scope.row.body }}</template>
      </el-table-column>
      <el-table-column align="center" label="管理" width="100">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit"  circle  @click=" () => {
                form = scope.row
                dialogVisible = true
              }  " />
          <el-button size="mini" type="danger" icon="el-icon-delete" circle @click="handleDelete(scope.row)" />
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="添加/修改文章分类" :visible.sync="dialogVisible" width="40%">
      <el-form label-width="100px">
        <el-form-item label="所属分类id">
          <div class="block">
            <el-cascader v-model="form.cid" :options="list" :show-all-levels="false" :props="{ children: 'child', value: 'id', label: 'typename', expandTrigger: 'hover', checkStrictly: true, emitPath: false }" clearable />
          </div>
        </el-form-item>
        <el-form-item label="分类名称">
          <el-input v-model="form.typename" />
        </el-form-item>
        <el-form-item label="分类介绍">
          <el-input v-model="form.body" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSubmit">立即提交</el-button>
          <el-button @click="dialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {},
      dialogVisible: false,
      list: [],
      query: {page:1,size:200}
    }
  },
  created() {
    this.initList()
  },
  methods: {
    initList() {
      this.$api.get('/api/basic/category', this.query).then((res) => {
        this.list = res.data
        this.query.total = res.total
      })
    },
    handleSubmit() {
      this.$api.put('/api/basic/category', {}, this.form).then((res) => {
        this.dialogVisible = false
        this.initList()
      })
    },
    handleDelete(row) {
      this.$api.deleteConfirm('/api/basic/category', { id: row.id }).then((res) => {
        this.initList()
      })
    },
    handleChange(row) {
      this.initList()
    },
    handleAdd(row) {}
  }
}
</script>

<style></style>
